package com.tokenmaster.aitrade.market;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class HttpUtilManager {

	private static Logger logger = Logger.getLogger(HttpUtilManager.class);

	private static HttpUtilManager instance = new HttpUtilManager();

	public static HttpUtilManager getInstance() {
		return instance;
	}

	// private static OkHttpClient httpClient = new
	// OkHttpClient.Builder().connectTimeout(20000, TimeUnit.MILLISECONDS)
	// .readTimeout(20000, TimeUnit.MILLISECONDS)
	// .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1",
	// 9666))).build();

	private static OkHttpClient httpClient = new OkHttpClient();

	public OkHttpClient getHttpClient() {
		return httpClient;
	}

	public void setHttpClient(OkHttpClient httpClient) {
		HttpUtilManager.httpClient = httpClient;
	}

	public static final MediaType JSON = MediaType.parse("application/json;charset=utf-8");

	public String requestHttpGet(String url_prex, String url, String param) throws IOException {

		url = url_prex + url;

		if (param != null && !param.equals("")) {
			if (url.endsWith("?")) {
				url = url + param;
			} else {
				url = url + "?" + param;
			}
		}

		Request request = new Request.Builder().url(url).build();
		Response response = httpClient.newCall(request).execute();
		return response.body().string(); // 返回的是string 类型，json的mapper可以直接处理
	}

	public String requestHttpPost(String url_prex, String url, String json) throws IOException {
		logger.info("POST请求" + url_prex + url + ", 入参是" + json);
		MediaType JSON = MediaType.parse("application/json;charset=utf-8");
		RequestBody requestBody = RequestBody.create(JSON, json);
		Request request = new Request.Builder().url(url_prex + url).post(requestBody).build();

		String result = httpClient.newCall(request).execute().body().string();

		logger.info("请求返回结果为" + result);
		return result;
	}

	public String requestHttpPost(String url_prex, String url, Map<String, String> params) throws IOException {

		okhttp3.FormBody.Builder builder = new okhttp3.FormBody.Builder();
		if (params != null) {
			Iterator<String> iterator = params.keySet().iterator();
			String key = "";
			while (iterator.hasNext()) {
				key = iterator.next().toString();
				logger.info(key + params.get(key));
				builder.add(key, params.get(key));
			}
		}

		Request request = new Request.Builder().url(url_prex + url).post(builder.build()).build();
		String result = "";
		result = httpClient.newCall(request).execute().body().string();

		logger.info("请求返回结果为" + result);
		return result;
	}

}
